package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把map转换成json
        String jsonString = JSONObject.toJSONString(paramMap);
        //把json转换成对象
        Department department = JSONObject.parseObject(jsonString, Department.class);
        //查询查看科室信息是否存在
        Department departmentExist =departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(departmentExist!=null){
            department.setId(departmentExist.getId());
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        //创建分页pageable对象
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        //0为第一页
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        //创建匹配器，如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true);//改变默认大小写忽略方式：忽略大小写


        Example<Department> example=Example.of(department,matcher);

       Page<Department> pages= departmentRepository.findAll(example,pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号、科室编号查询科室
        Department department =
                departmentRepository
                        .getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        //根据id删除
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }
        //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建最终返回对象
        List<DepartmentVo> result=new ArrayList<>();
        //根据医院编号 查询所有科室信息
        Department department=new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);
         //处理科室集合，根据大科室id 进行划分， 操作集合数组 更方便  利用流式计算
        //想象处理后结果 是一个map 最好是key是bigcode value是大科室下所有小科室的集合
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
            //遍历map集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //封装大科室信息
            DepartmentVo bigDeptVo=new DepartmentVo();
            bigDeptVo.setDepcode(entry.getKey());
            bigDeptVo.setDepname(entry.getValue().get(0).getBigname());
            //封装小科室信息，创建封装小科室的集合
            List<DepartmentVo> deptVoList=new ArrayList<>();
           List<Department>  deptList=entry.getValue();
           //遍历进行封装
            for (Department dept : deptList) {
                DepartmentVo departmentVo= new DepartmentVo();
                BeanUtils.copyProperties(dept,departmentVo);
                deptVoList.add(departmentVo);
            }
            //小科室集合存入大科室对象
            bigDeptVo.setChildren(deptVoList);
            //大科室对象存入最终返回的集合
            result.add(bigDeptVo);

        }
        return result;
    }
    //根据医院编号 科室编号 获取科室 名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository
                .getDepartmentByHoscodeAndDepcode(hoscode,depcode);

        if(department!=null){
            return department.getDepname();
        }
        return "";

    }

    //根据医院编号 科室编号 获取科室信息
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository
                .getDepartmentByHoscodeAndDepcode(hoscode,depcode);

        return department;
    }

}
